<template>
  <div class="approval">
    <el-card class="box-card" style="margin-top: 110px;">
      <div class="head-title2">
        <div class='title'>
          {{$t('achievements.title1')}}
        </div>
        <div class="info">
          <el-tooltip placement="left">
            <div slot="content">
              {{define}}
            </div>
            <div class="btns"></div>
          </el-tooltip>
        </div>
      </div>
      <div class="menu-search">
        <div class="menu-title">
          <div class="searchItem searchItem-date">
            <span>{{$t('LoanReport.DateLoan')}}</span>
            <!-- 放款开始时间 -->
            <el-date-picker
              size='small'
              v-model="listQuery.lendStartDate"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('rule.beginDate')"
            ></el-date-picker>
            <b>{{$t('btn.go')}}</b>
            <!-- 放款结束时间 -->
            <el-date-picker
              size='small'
              v-model="listQuery.lendEndDate"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('rule.endDate')"></el-date-picker>
          </div>
          <div class="searchItem">
            <span>{{$t('m1addm3Report.observeDate')}}</span>
            <!-- 观察时间 -->
            <el-date-picker
              size="small"
              v-model="listQuery.observeDate"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('m1addm3Report.choiceDate')"></el-date-picker>
          </div>
          <div class="searchItem">
            <span>{{$t('expireRemind.proTerm')}}</span>
            <!-- 产品期限 -->
            <el-select v-model="listQuery.productDueTime" size="small">
              <el-option
                v-for="item in productTerm"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </div>
          <div class="searchItem">
            <span>{{$t('LoanReport.customerType')}}</span>
            <!-- 用户类型 -->
            <el-select v-model="listQuery.userType" size="small">
              <el-option
                v-for="item in userTypeArr"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </div>
          <div class="searchItem">
            <span>{{$t('LoanReport.userGrade')}}</span>
            <!-- 用户等级 -->
            <el-select
              v-model="listQuery.userGrade"
              size="small">
              <el-option
                v-for="item in gradeArr"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </div>
          
          <div class="searchItem searchItem-date">
            <span>{{$t('LoanReport.DateUnit')}}</span>
            <!-- 日期单位 -->
            <el-radio-group v-model="listQuery.dateUnit" size="small">
              <el-radio-button label="3" value="3">{{$t('creditSupermarket.month')}}</el-radio-button>
              <el-radio-button label="2" value="2">{{$t('LoanReport.week')}}</el-radio-button>
              <el-radio-button label="1" value="1">{{$t('LoanReport.daily')}}</el-radio-button>
            </el-radio-group>
          </div>
          <div class="searchItem radioGroup">
            <span>{{$t('m1addm3Report.repayDay')}}</span>
            <!-- 应还款日 -->
            <el-radio-group v-model="listQuery.repayDayType" size="small">
              <el-radio-button label="1" value="1">{{$t('m1addm3Report.fisrtRePayDay')}}</el-radio-button>
              <el-radio-button label="2" value="2">{{$t('m1addm3Report.lastRePayDay')}}</el-radio-button>
            </el-radio-group>
          </div>
        </div>
        <div class="menu-title-btn">
          <button type="button" @click="search">{{$t('btn.query')}}</button>
          <button type="button" @click="reset">{{$t('btn.reset')}}</button>
          <button
            type="button"
            @click="handleDownload"
            :disabled="expDis"
            v-if="dpdreport_btn_export"
          >{{$t('btn.export')}}</button>
        </div>
      </div>
    </el-card>
    <!-- 信息总览 -->
    <el-card class="box-card" style="margin-top: 10px;">
      <div class="title">{{$t('LoanReport.InfoOverview')}}</div>
      <div class="info-overview">
        <!-- 放款数 -->
        <div class="info-item">
          <div class="info-circle">{{info.lendTotal}}</div>
          <div class="info-text">{{$t('m1addm3Report.lendTotal')}}</div>
        </div>
        <!-- 合同金额 -->
        <div class="info-item">
          <div class="info-circle">{{info.lendPrice | toThousandslsFilter}}</div>
          <div class="info-text">{{$t('m1addm3Report.lendPrice')}}</div>
        </div>
        <!-- DPD数 -->
        <div class="info-item">
          <div class="info-circle">{{info.dpdTotal}}</div>
          <div class="info-text">{{$t('DPDReport.dpdTotal')}}</div>
        </div>
        <!-- DPD金额 -->
        <div class="info-item">
          <div class="info-circle">{{info.dpdPrice | toThousandslsFilter}}</div>
          <div class="info-text">{{$t('DPDReport.dpdPrice')}}</div>
        </div>
        <!-- DPD笔数率 -->
        <div class="info-item">
          <div class="info-circle">{{info.dpdTotalPre}}</div>
          <div class="info-text">{{$t('DPDReport.dpdTotalPre')}}</div>
        </div>
        <!-- DPD金额率 -->
        <div class="info-item">
          <div class="info-circle">{{info.dpdPriceOverduePre}}</div>
          <div class="info-text">{{$t('DPDReport.dpdPriceOverduePre')}}</div>
        </div>
      </div>
    </el-card>
    <!-- 明细列表 -->
    <el-card class="box-card" style="margin-top: 10px;margin-bottom: 20px;">
      <div class="title">{{$t('DailyStatistics.ListDetails')}}</div>
      <el-table
        v-loading="listLoading"
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%; margin-top: 5px;"
        @selection-change="handleSelectionChange"
        border
        size="mini"
      >
        <!-- 时间 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="amountDate"
          :label="$t('m1addm3Report.amountDate')"
        ></el-table-column>
        <!-- 放款数 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="lendTotal"
          :label="$t('m1addm3Report.lendTotal')"
        ></el-table-column>
        <!-- 合同金额 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="lendPrice"
          :formatter="cashFormatter"
          :label="$t('m1addm3Report.lendPrice')"
        ></el-table-column>
        <!-- DPD数 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="dpdTotal"
          :label="$t('DPDReport.dpdTotal')"
        ></el-table-column>
        <!-- DPD笔数率 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="dpdTotalPre"
          :label="$t('DPDReport.dpdTotalPre')"
        ></el-table-column>
        <!-- DPD金额 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="dpdPrice"
          :formatter="cashFormatter"
          :label="$t('DPDReport.dpdPrice')"
        ></el-table-column>
        <!-- DPD金额率 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="dpdPriceOverduePre"
          :label="$t('DPDReport.dpdPriceOverduePre')"
        ></el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.page"
          :page-sizes="[5, 10, 20, 30]"
          :page-size="listQuery.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { mapGetters } from 'vuex';
import { padLeftZero, formatCash } from 'utils/index'
import { getProductDueList, getInvestorDPDReportPage, exportInvestorDPDReport, getInvestorDPDReportScreen } from 'api/reportStatistics/investorReport/index';
export default {
  data() {
    return {
      define: '',
      // changeDate: [new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 7), new Date()],
      dpdreport_btn_export: false,
      listLoading: false,
      total: null,
      expDis: true,
      // 查询条件
      listQuery: {
        limit: 5,
        page: 1,
        // 放款开始时间
        lendStartDate: '',
        // 放款结束时间
        lendEndDate: '',
        // 观察时间
        observeDate: '',
        // 产品期限
        productDueTime: '',
        // 用户类型
        userType: '-1',
        // ⽤户等级
        userGrade: '',
        // 日期单位
        dateUnit: 3,
        // 应还款⽇
        repayDayType: '1'
      },
      tableData: [],
      multipleSelection: [],
      productTerm: [],
      userTypeArr: [
        {
          id: '-1',
          name: this.$t('rule.all')
        },
        {
          id: '1',
          name: this.$t('LoanReport.newUser')
        },
        {
          id: '2',
          name: this.$t('LoanReport.oldUser')
        }
      ],
      gradeArr: [
        {
          id: '',
          name: this.$t('rule.all')
        }
      ],
      info: {
        lendTotal: 0,
        lendPrice: 0,
        dpdTotal: 0,
        dpdTotalPre: '',
        dpdPrice: 0,
        dpdPriceOverduePre: ''
      }
    };
  },
  computed: {
    ...mapGetters(['elements', 'DPDReportPage', 'getReportDefine'])
  },
  created() {
    if (this.DPDReportPage.has(this.$route.path)) {
      this.listQuery = this.DPDReportPage.get(this.$route.path);
    }
    this.getTime();
    this.getList();
    // 产品期限
    getProductDueList().then(res => {
      this.productTerm = res.data.productDueList;
    });
    this.dpdreport_btn_export = this.elements['DPDReport:btn_export'];
    // 获取报表定义
    this.getReportDefine.forEach(item => {
      if (item.dictCode === 'DPDREPORT') {
        this.define = item.dictValue
      }
    })
  },
  methods: {
    cashFormatter(row, col, cellValue) {
      return formatCash(cellValue)
    },
    // 点击导出
    handleDownload() {
      this.listLoading = true;
      exportInvestorDPDReport(this.listQuery).then(res => {
        require.ensure([], () => {
          const { export_json_to_excel } = require('@/vendor/Export2Excel');
          const tHeader = [
            this.$t('m1addm3Report.amountDate'),
            this.$t('m1addm3Report.lendTotal'),
            this.$t('m1addm3Report.lendPrice'),
            this.$t('DPDReport.dpdTotal'),
            this.$t('DPDReport.dpdTotalPre'),
            this.$t('DPDReport.dpdPrice'),
            this.$t('DPDReport.dpdPriceOverduePre')
          ];
          const filterVal = [
            'amountDate',
            'lendTotal',
            'lendPrice',
            'dpdTotal',
            'dpdTotalPre',
            'dpdPrice',
            'dpdPriceOverduePre'
          ];
          const list = res.data.rows;
          const data = this.formatJson(filterVal, list);
          export_json_to_excel(tHeader, data, this.$t('route.DPDReport'));
          this.listLoading = false;
        });
      });
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]));
    },
    // 获取查询数据
    getList() {
      this.listLoading = true;
      // 保存查询条件到vuex key=path value=object
      this.$store.dispatch('saveDPDReportPage', {
        path: this.$route.path,
        listQuery: this.listQuery
      });
      // 信息总览
      getInvestorDPDReportScreen(this.listQuery).then(res => {
        if (res && res.data) {
          this.info = Object.assign({}, this.info, res.data)
        }
      })
      // 分页数据
      getInvestorDPDReportPage(this.listQuery).then(res => {
        if (res.data && res.data.rows) {
          this.tableData = res.data.rows
          this.total = parseInt(res.data.total || 0)
          this.listLoading = false;
          this.expDis = false;
        }
      });
    },
    getTime() {
      this.listQuery.lendStartDate = this.GetDateStr(-7);
      this.listQuery.lendEndDate = this.GetDateStr(0);
      this.listQuery.observeDate = this.GetDateStr(0);
    },
    GetDateStr(AddDayCount) {
      const dd = new Date();
      dd.setDate(dd.getDate() + AddDayCount);// 获取AddDayCount天后的日期
      const y = dd.getFullYear();
      const m = padLeftZero(dd.getMonth() + 1, 2);
      const d = padLeftZero(dd.getDate(), 2);
      return y + '-' + m + '-' + d;
    },
    // 查询
    search() {
      if (!this.listQuery.lendStartDate || !this.listQuery.lendEndDate) {
        this.$message({
          showClose: true,
          type: 'warning',
          message: this.$t('LoanReport.warnDateLoan')
        });
      } else if (this.listQuery.observeDate
                  && this.listQuery.lendEndDate
                  && !this.compareDate(this.listQuery.observeDate, this.listQuery.lendEndDate)) {
        this.$message({
          showClose: true,
          type: 'warning',
          message: this.$t('LoanReport.warnDateLoan2')
        });
      } else {
        this.listQuery.page = 1;
        this.getList();
      }
    },
    // 比较2个字符串日期
    compareDate(date1, date2) {
      if (date1 && date2) {
        return new Date(date1).getTime() >= new Date(date2).getTime()
      } else {
        return false
      }
    },
    // 重置
    reset() {
      this.listQuery.limit = 5;
      this.listQuery.page = 1;
      this.getTime()
      // this.listQuery.observeDate = ''
      this.listQuery.productDueTime = ''
      this.listQuery.userType = '-1'
      this.listQuery.userGrade = ''
      this.listQuery.dateUnit = 3
      this.listQuery.repayDayType = '1'
    },
    // 选中的数据
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // limit事件
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    // page事件
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    }
  }
};
</script>

<style scoped lang='scss'>
.box-card {
  width: 95%;
  margin-left: 2.5%;
  margin-right: 2.5%;
}
.title {
  line-height: 30px;
  font-size: 16px;
  display: flex;
  align-items: center;
  font-weight: bold;
}
.title:before {
  display: block;
  content: '';
  width: 3px;
  height: 16px;
  background-color: #309afe;
}
.title:before {
  margin-right: 8px;
}
::before {
  box-sizing: inherit;
  -webkit-tap-highlight-color: transparent;
}
.menu-title {
  width: 100%;
  box-sizing: border-box;
  background: #ffffff;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .searchItem {
    display: flex;
    align-items: center;
    padding: 10px 0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 150px;
    }
    &.radioGroup {
      div:nth-child(2) {
        width: auto;
      }
    }
  }
  // .searchItem-date {
  //   display: flex;
  //   align-items: center;
  //   padding: 10px 0;
  //   span {
  //     display: inline-block;
  //     width: 100px;
  //     text-align: right;
  //     font-size: 12px;
  //     color: #5f5f61;
  //     padding-right: 5px;
  //   }
  //   div:nth-child(2) {
  //     width: 220px;
  //   }
  // }
  .searchItem-date {
    display: flex;
    align-items: center;
    padding: 10px  0;
    b {
      margin: 0 10px;
      font-weight: normal;
      font-size: 12px;
      color: #5f5f61;
    }
    div {
       width: 150px;
    }
  }
}
.menu-title-btn {
  padding: 10px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
    &:nth-child(2) {
      background: #ebf5ff;
      color: #20a0ff;
      border: 1px solid #abd5ff;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.menu-list {
  padding: 20px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.info-overview {
  display: flex;
  justify-content: space-around;
  text-align: center;
  .info-item {
    flex: 1;
    .info-circle {
      width: 130px;
      height: 130px;
      line-height: 130px;
      text-align: center;
      border-radius: 50%;
      border: 2px solid rgb(129, 125, 125);
      margin-top: 5px;
      margin-bottom: 8px;
      margin-left: auto;
      margin-right: auto;
      font-size: 18px;
      font-weight: bold;
    }
    .info-text {
      font-size: 14px;
      line-height: 20px;
    }
  }
}
.head-title2 {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.btns {
  display: inline-block;
  width: 24px;
  height: 24px;
  background-size: cover;
  background-image: url("../../../../assets/images/question-mark.svg");
}
</style>